package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyOrderProductAddBo;
import com.ruoyi.system.bo.ZsyyOrderProductQueryBo;
import com.ruoyi.system.bo.ZsyyOrderProductEditBo;
import com.ruoyi.system.domain.ZsyyOrderProduct;
import com.ruoyi.system.mapper.ZsyyOrderProductMapper;
import com.ruoyi.system.vo.ZsyyOrderProductVo;
import com.ruoyi.system.service.IZsyyOrderProductService;
import com.ruoyi.system.mapper.ZsyyOrderProductMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 订单关联产品Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyOrderProductServiceImpl extends ServicePlusImpl<ZsyyOrderProductMapper, ZsyyOrderProduct> implements IZsyyOrderProductService {
    @Autowired
    private ZsyyOrderProductMapper iZsyyOrderProductMapper;

    @Override
    public ZsyyOrderProductVo queryById(Long id){
        return getVoById(id, ZsyyOrderProductVo.class);
    }

    @Override
    public TableDataInfo<ZsyyOrderProductVo> queryPageList(ZsyyOrderProductQueryBo bo) {
        PagePlus<ZsyyOrderProduct, ZsyyOrderProductVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyOrderProductVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyOrderProductVo> queryList(ZsyyOrderProductQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyOrderProductVo.class);
    }

    private LambdaQueryWrapper<ZsyyOrderProduct> buildQueryWrapper(ZsyyOrderProductQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyOrderProduct> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, ZsyyOrderProduct::getProductId, bo.getProductId());
        lqw.eq(bo.getCourseId() != null, ZsyyOrderProduct::getCourseId, bo.getCourseId());
        lqw.eq(bo.getOrderId() != null, ZsyyOrderProduct::getOrderId, bo.getOrderId());
        lqw.eq(StrUtil.isNotBlank(bo.getOrderNo()), ZsyyOrderProduct::getOrderNo, bo.getOrderNo());
        lqw.like(StrUtil.isNotBlank(bo.getProductName()), ZsyyOrderProduct::getProductName, bo.getProductName());
        lqw.eq(bo.getNum() != null, ZsyyOrderProduct::getNum, bo.getNum());
        lqw.eq(bo.getPreferentialPrice() != null, ZsyyOrderProduct::getPreferentialPrice, bo.getPreferentialPrice());
        lqw.eq(bo.getPrice() != null, ZsyyOrderProduct::getPrice, bo.getPrice());
        lqw.eq(StrUtil.isNotBlank(bo.getImg()), ZsyyOrderProduct::getImg, bo.getImg());
        lqw.eq(bo.getProductType() != null, ZsyyOrderProduct::getProductType, bo.getProductType());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyOrderProductAddBo bo) {
        ZsyyOrderProduct add = BeanUtil.toBean(bo, ZsyyOrderProduct.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyOrderProductEditBo bo) {
        ZsyyOrderProduct update = BeanUtil.toBean(bo, ZsyyOrderProduct.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyOrderProduct entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
